package com.excellence.service.impl;

import com.excellence.mapper.NewsMapper;
import com.excellence.pojo.News;
import com.excellence.service.NewsService;

import com.excellence.utils.SaveFilesPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author 沈秉璋
 * @ClassName NewsServiceImpl
 * @Description TODO
 * @date 2024/4/8 10:50
 * @Version 1.0
 */
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    NewsMapper newsMapper;
    @Override
    public int deleteByPrimaryKey(Long id) {
        return newsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByKey(Long id) {
        List<News> record = newsMapper.selectByNewsType(String.valueOf(id));
        record.get(0).setDeleted(1);
        return newsMapper.updateByPrimaryKey(record.get(0));
    }
    @Override
    public int insert(News record,  List<MultipartFile> files) {
        if (files != null) {
            record.setNewsImage(SaveFilesPath.saveFilesPath(files));
        }
        record.setCreateTime(new Date());
        return newsMapper.insert(record);
    }

    @Override
    public int insertSelective(News record,  List<MultipartFile> files) {
        if (files != null) {
            record.setNewsImage(SaveFilesPath.saveFilesPath(files));
        }
        record.setCreateTime(new Date());
        return newsMapper.insertSelective(record);
    }

    @Override
    public List<News> selectByNewsType(String newsType) {
        return newsMapper.selectByNewsType(newsType);
    }

    @Override
    public List<News> selectAll() {
        return newsMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKeySelective(News record,  List<MultipartFile> files) {
        if (files != null) {
            record.setNewsImage(SaveFilesPath.saveFilesPath(files));
        }
        return newsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(News record,  List<MultipartFile> files) {
        if (files != null) {
            record.setNewsImage(SaveFilesPath.saveFilesPath(files));
        }
        return newsMapper.updateByPrimaryKey(record);
    }
}
